using System.Threading.Tasks;
using UnityEngine;
using System.Threading;

namespace EntityKit.Runtime
{
    public static class AssetHelper
    {
        private static IAssetLoader mAssetLoader;

        public static void Initialize(IAssetLoader assetLoader)
        {
            mAssetLoader = assetLoader;
        }

        public static void Release()
        {
            if (mAssetLoader == null) return;
            UnloadUnusedAssets();
            mAssetLoader = null;
        }

        /// <summary>
        /// 同步加载资源
        /// </summary>
        /// <param name="location">资源路径</param>
        /// <typeparam name="T">资源类型</typeparam>
        /// <returns>资源</returns>
        public static T LoadAssetSync<T>(string location) where T : Object
        {
            if (mAssetLoader != null) return mAssetLoader.LoadAssetSync<T>(location);
            Debug.LogError("AssetHelper not initialize.");
            return null;
        }

        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="location">资源路径</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <typeparam name="T">资源类型</typeparam>
        /// <returns>资源</returns>
        public static async Task<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default) where T : Object
        {
            if (mAssetLoader != null) return await mAssetLoader.LoadAssetAsync<T>(location, cancellationToken);
            Debug.LogError("AssetHelper not initialize.");
            return null;
        }

        /// <summary>
        /// 同步实例化GameObject
        /// </summary>
        /// <param name="location">GameObject路径</param>
        /// <param name="parent">父级</param>
        /// <returns></returns>
        public static GameObject InstantiateGameObjectSync(string location, Transform parent = null)
        {
            if (mAssetLoader != null) return mAssetLoader.InstantiateGameObjectSync(location, parent);
            Debug.LogError("AssetHelper not initialize.");
            return null;
        }

        /// <summary>
        /// 异步实例化GameObject
        /// </summary>
        /// <param name="location">GameObject路径</param>
        /// <param name="parent">父级</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        public static async Task<GameObject> InstantiateGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default)
        {
            if (mAssetLoader != null) return await mAssetLoader.InstantiateGameObjectAsync(location, parent, cancellationToken);
            Debug.LogError("AssetHelper not initialize.");
            return null;
        }

        /// <summary>
        /// 卸载未使用的资源
        /// </summary>
        public static void UnloadUnusedAssets()
        {
            if (mAssetLoader != null) mAssetLoader.UnloadUnusedAssets();
            else Debug.LogError("AssetHelper not initialize.");
        }
    }
}